package login;

import javax.swing.JFrame;


public class LoginHandler {
        
        private UserCon users;
        private Login login;
        private Register register;
        
        public LoginHandler( JFrame frame ) {
                users = UserCon.getInstance();
                this.readUsers();
                login = new Login( frame, this );
                login.setEnabled( true );
                login.setVisible( true );
                
        }
        
        public void validateLogIn() {
                User u = users.getUser(login.getUsername());
                if( u == null )
                        login.setFeedback( "The username you have entered does not exist." );
                else if( !u.isPassword(this.passwordToString(login.getPassword())) ) 
                        login.setFeedback( "The password you have entered is incorrect." );
                else
                        login.setVisible( false );
        }
        
        private String passwordToString( char[] password ) {
                String pwd = "";
                for( int i=0; i<password.length; i++ ) {
                        pwd += password[i];
                }
                return pwd;
        }
        
        public void createNewAccount() {
                login.setVisible(false);
                register = new Register( (JFrame)login.getOwner(), this );
                register.setVisible(true);
        }
        
        public void validateRegister() {
                String uname = register.getUsername();
                User u = users.getUser(uname);
                String pwd1 = this.passwordToString(register.getPassword1());
                if( u != null ) 
                        register.setFeedback( "The username you have entered already exists" );
                else if( uname.length() < 3 ) 
                        register.setFeedback( "You have to enter a username with at least 3 signs" );
                else if( pwd1.length() < 6 )
                        register.setFeedback( "You have to enter a password with at least 6 signs" );
                else if( (this.passwordToString(register.getPassword1()).length()<6) || !(pwd1.equals(this.passwordToString(register.getPassword1())))  )
                        register.setFeedback( "The validation of your password has to be the same as your password" );
                else {
                        users.createNewUser(register.getUsername(), pwd1 );
                        register.setVisible(false);
                }
                
        }
        
        public void writeUsers() {
                ReadWrite.writeUsers( users.getUsers() );
        }
        
        public void readUsers() {
                users.setUsers( ReadWrite.readUsers() );
        }
        
        
        /**Prepares the object for garbage collector.*/
        @Override
        public void finalize() throws Throwable {
                this.writeUsers();
                super.finalize();
        }
        
}